//
//  ShowMyFamilyDetialViewController.m
//  安好
//
//  Created by lianchuang on 14-12-1.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "ShowMyFamilyDetialViewController.h"
//展示家人头像
#import "FamilyInfoFirstSectionCell.h"
//具体cell
#import "AddFamilyRemindCell.h"
//push到编辑页面
#import "EditMyFamilyDetialViewController.h"

/**
 * TakeDrugsViewController 用药提醒家人列表页面
 * 如果是从“用药提醒”页面过来的，当前展示页面返回的时候就会返回到用药提醒页面
 * 用药提醒家人列表--》用药提醒--》当前“家人详情”页面
 */
#import "TakeDrugsViewController.h"

/**
 * MyFamilyViewController 我的家人蜂窝页面
 * 如果是从“我的家人”蜂窝页面过来的，那么当前展示页面返回就要返回到蜂窝页面
 * 我的家人蜂窝页面--》我的家人“病历”“用药提醒”--》当前“家人详情”页面
 */
#import "MyFamilyViewController.h"

/**
 *  咨询--添加完家人成功后到了这个家人详情展示页面
 *  - 如果返回还是返回到咨询模块的家人列表页面
 */
#import "ConsultFamilyViewController.h"


#define IPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)


@interface ShowMyFamilyDetialViewController ()<UITableViewDataSource,UITableViewDelegate,EditMyFamilyDetialViewControllerDelegate>
//展示页面的table
@property(nonatomic,strong)UITableView * tableView;

/**
 * gender,birthday 这个数组放一个一个字典只有当对应的值不为空才放进去
 性别:gender  出生年月:birthday
 */

@property(nonatomic,strong)NSArray * optionOneArray;
/**
 *  mobile,idcard 这个数组方一个一个字典
 @"手  机  号"   @"身  份  证"
 */
@property(nonatomic,strong)NSArray * optionTwoArray;

/**
 *  性别
 */
@property(nonatomic,copy)NSString * gender;
/**
 *  生日
 */
@property(nonatomic,copy)NSString * birthday;
/**
 *  身份证
 */
@property(nonatomic,copy)NSString * idcard;
/**
 * 电话号
 */
@property(nonatomic,copy)NSString * mobile;
/**
 *  选填内容
 */
@property(nonatomic,strong)NSMutableArray * thirdDataArray;

@end

@implementation ShowMyFamilyDetialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    [self makeNavBar];
    [self makeTableView];
    
}

-(void)loadData
{
    self.thirdDataArray = [NSMutableArray arrayWithCapacity:0];
    self.gender = self.familyAllDetialDic[@"gender"];
    self.birthday = self.familyAllDetialDic[@"birthday"];
    self.mobile = self.familyAllDetialDic[@"mobile"];
    self.idcard = self.familyAllDetialDic[@"idcard"];
    
    if ([self.gender intValue] != 0) {
        [self.thirdDataArray addObject:@{@"gender":self.familyAllDetialDic[@"gender"]}];
    }
    if ([self.birthday length] != 0) {
        [self.thirdDataArray addObject:@{@"birthday":self.familyAllDetialDic[@"birthday"]}];
    }
    if ([self.mobile length] != 0) {
        [self.thirdDataArray addObject:@{@"mobile":self.familyAllDetialDic[@"mobile"]}];
    }
    if ([self.idcard length] != 0) {
        [self.thirdDataArray addObject:@{@"idcard":self.familyAllDetialDic[@"idcard"]}];
    }
    
}
#pragma mark - 导航条设置
-(void)makeNavBar
{
    self.navigationItem.title = @"家人详情";
    // 返回按钮图片
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnClicked)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
    // 完成
    UIBarButtonItem *EditBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_编辑.png"] style:UIBarButtonItemStylePlain target:self action:@selector(editButtonClicked)];
    EditBtnImg.tintColor = [UIColor whiteColor];
    EditBtnImg.tag = 100;
    self.navigationItem.rightBarButtonItem = EditBtnImg;
}

#pragma mark - ----编辑页面的代理方法
-(void)reloadShowFamilyDetialWithFamilyDic:(NSDictionary *)familyDic
{
    self.familyAllDetialDic = familyDic;
    //重新给赋值
    [self loadData];
    //刷新table
    [self.tableView reloadData];
}

/**
 *  返回
 */
-(void)backBtnClicked
{
    if (self.isPresent) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        
        if([[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3] isKindOfClass:[MyFamilyViewController class]]){
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3] animated:YES];
        }else if ([[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3] isKindOfClass:[TakeDrugsViewController class]]) {
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3] animated:YES];
        }else if ([[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3] isKindOfClass:[ConsultFamilyViewController class]]){
            [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3] animated:YES];
        }
    
    }
}
/**
 *  push-->编辑家人信息
 */
-(void)editButtonClicked
{
    EditMyFamilyDetialViewController * edit = [[EditMyFamilyDetialViewController alloc] init];
    //把传到展示页面的值再原封不动传给编辑页面
    //传数组会快一些
    NSArray * array = @[self.familyAllDetialDic];
    edit.familyArray = array;
    
    edit.delegate = self;
    edit.isPresent = self.isPresent;
    [self.navigationController pushViewController:edit animated:YES];
    
}

-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    [self.tableView setSeparatorColor:[UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1]];
    [self.view addSubview:self.tableView];

}

#pragma mark - table的代理方法
//多少段
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    if (self.thirdDataArray.count == 0) {
        return 2;
    }else
    {
        return 3;
    }
    
}
//各段有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 3;
    }else
    {
        return self.thirdDataArray.count;//原来是2，改为4
    }
}
//设置段头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (IPhone6Plus) {
        return 12;
    }else{
        return 10;
    }
    
    
}
//段尾
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
//段头视图
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
//各行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)//第0段 64
    {
        if (IPhone6Plus) {
            return 78;
        }else{
            return 64;
        }
        
    }
    else{
        if (IPhone6Plus) {
            return 53;
        }else{
            return 44;
        }
    }

}
#pragma mark ----cell--加载
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)//第一段只展示家人的头像
    {
        FamilyInfoFirstSectionCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Header"];
        if (cell == nil) {
            if (IPhone6Plus) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"FamilyInfoFirstSectionCell" owner:self options:nil] lastObject];
            }else{
                cell = [[[NSBundle mainBundle] loadNibNamed:@"FamilyInfoFirstSectionCell" owner:self options:nil] firstObject];
            }
        }
        cell.familyHeaderImgView.clipsToBounds = YES;
        cell.familyHeaderImgView.layer.cornerRadius = 3;
        UIView * top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
        [cell.contentView addSubview:top];
        UIView * bottom = [[UIView alloc] initWithFrame:CGRectMake(0, cell.height-0.5, kScreenWidth, 0.5)];
        [cell.contentView addSubview:bottom];
        
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        //文件夹地址
        NSString * headerImgFloder = [docPath stringByAppendingString:[NSString stringWithFormat:@"/FamilyHeaderFloder/"]];
        //图片地址
        NSString * newPicPath = [headerImgFloder stringByAppendingString:[NSString stringWithFormat:@"headerImg%@%@.jpg",self.familyAllDetialDic[@"uid"],self.familyAllDetialDic[@"fid"]]];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        //如果这个文件夹存并且这个图片存在就进行下一步操作
        if ([fileManager fileExistsAtPath:headerImgFloder] && [fileManager fileExistsAtPath:newPicPath]) {
            UIImage * img = [UIImage imageWithContentsOfFile:newPicPath];
            if (img != nil) {
                [cell.familyHeaderImgView setImage:img];
            }else{
                [cell.familyHeaderImgView setImageWithURL:[NSURL URLWithString:self.familyAllDetialDic[@"avatar"]] placeholderImage:[UIImage imageNamed:@"默认头像"]];
            }
            
        }else{
            [cell.familyHeaderImgView setImageWithURL:[NSURL URLWithString:self.familyAllDetialDic[@"avatar"]] placeholderImage:[UIImage imageNamed:@"默认头像"]];
        }

                
        cell.jumpImgView.hidden = YES;
        cell.familyNameLable.hidden = YES;
        
        
        return cell;
    }
    else//
    {
        AddFamilyRemindCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
        if (cell == nil) {
            if (IPhone6Plus) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"AddFamilyRemindCell" owner:self options:nil] lastObject];
            }else{
                cell = [[[NSBundle mainBundle] loadNibNamed:@"AddFamilyRemindCell" owner:self options:nil] firstObject];
            }
        }
        cell.jumpImgView.hidden = YES;
        
        
        if (indexPath.section == 1)//第二段：必填内容
        {
            if (indexPath.row == 0) {
                cell.topLineView.frame = CGRectMake(0, 0, kScreenWidth, 0.5);
            }else
            {
                cell.topLineView.hidden = YES;
            }
            
            if (indexPath.row != 2) {
                if (IPhone6Plus) {
                     cell.bottomLineView.frame = CGRectMake(20, cell.height-0.5, kScreenWidth-20, 0.5);
                }else{
                    cell.bottomLineView.frame = CGRectMake(15, cell.height-0.5, kScreenWidth-15, 0.5);
                }
            }else
            {
               
                cell.bottomLineView.frame = CGRectMake(0, cell.height-0.5, kScreenWidth, 0.5);
            }
            
            if (indexPath.row == 0) {
                cell.familyReminderLable.text = @"真实姓名";
                cell.showGetInforLable.text = self.familyAllDetialDic[@"realname"];
            }
            if (indexPath.row == 1) {
                NSArray * typeArray = @[@"祖父母", @"父母",@"其他平辈", @"子女", @"孙辈",@"爱人"];
                cell.familyReminderLable.text = @"关       系";
                cell.showGetInforLable.text = [typeArray objectAtIndex:[self.familyAllDetialDic[@"type"] intValue]-1];
            }
            if (indexPath.row == 2) {
                cell.familyReminderLable.text = @"昵       称";
                cell.showGetInforLable.text = self.familyAllDetialDic[@"username"];
            }
        }
        //选填内容
        if (indexPath.section == 2)
        {
            NSDictionary * dic = self.thirdDataArray[indexPath.row];
            if ([[[dic allKeys]objectAtIndex:0] isEqualToString:@"gender"]) {
                NSArray * genderArray = @[@"未知",@"男",@"女"];
                cell.familyReminderLable.text = @"性       别";
                cell.showGetInforLable.text = [genderArray objectAtIndex:[self.gender intValue]];
            }
            if ([[[dic allKeys]objectAtIndex:0] isEqualToString:@"birthday"]){
                cell.familyReminderLable.text = @"出生年月";
                cell.showGetInforLable.text = self.birthday;
            
            }
            if ([[[dic allKeys]objectAtIndex:0] isEqualToString:@"mobile"]){
                cell.familyReminderLable.text = @"手  机  号";
                cell.showGetInforLable.text = self.mobile;
            }
            if ([[[dic allKeys]objectAtIndex:0] isEqualToString:@"idcard"]){
                cell.familyReminderLable.text =@"身  份  证";
                cell.showGetInforLable.text = self.idcard;
            }
        }
        return cell;
    }
    
}


@end
